package com.ruoyi.system.controller;

import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.SysOrganizationDomainChildren;
import com.ruoyi.system.domain.SysOrganizationDomainLabel;
import com.ruoyi.system.domain.dto.SysOrganizationDomainDto;
import com.ruoyi.system.domain.vo.SysOrganizationDomainVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.SysOrganizationDomain;
import com.ruoyi.system.service.ISysOrganizationDomainService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;


@Api(value = "组织领域", tags = "组织领域Controller")
@RestController
@RequestMapping("/domain")
public class SysOrganizationDomainController extends BaseController {
    @Autowired
    private ISysOrganizationDomainService sysOrganizationDomainService;


    //    @RequiresPermissions("system:domain:list")
    @ApiOperation("查询组织领域列表 结果是 树形嵌套")
    @GetMapping("/list")
    public TableDataInfo list(SysOrganizationDomainDto sysOrganizationDomain) {
        startPage();
        List<SysOrganizationDomain> list = sysOrganizationDomainService.selectSysOrganizationDomainList(sysOrganizationDomain);
        return getDataTable(list);
    }


    @ApiOperation("查询组织领域列表")
    @GetMapping("/queryDomainLabelList")
    public TableDataInfo queryDomainLabelList(SysOrganizationDomainDto sysOrganizationDomain) {
        startPage();
        List<SysOrganizationDomainLabel> list = sysOrganizationDomainService.queryDomainLabelList(sysOrganizationDomain);
        return getDataTable(list);
    }


    @ApiOperation("区域领域信息配置 平铺查询")
    @GetMapping("/queryDomainList")
    public TableDataInfo queryDomainList(SysOrganizationDomainDto sysOrganizationDomain) {
        startPage();
        List<SysOrganizationDomainVo> list = sysOrganizationDomainService.queryDomainList(sysOrganizationDomain);
        return getDataTable(list);
    }

    /**
     * 获取组织领域详细信息
     */
//    @RequiresPermissions("system:domain:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return success(sysOrganizationDomainService.selectSysOrganizationDomainById(id));
    }


//    @RequiresPermissions("system:domain:edit")
//    @Log(title = "组织领域", businessType = BusinessType.UPDATE)
    @ApiOperation("新增 修改组织领域")
    @PutMapping
    public AjaxResult edit(@RequestBody SysOrganizationDomain sysOrganizationDomain) {
        return toAjax(sysOrganizationDomainService.updateSysOrganizationDomain(sysOrganizationDomain));
    }

    @ApiOperation("删除组织领域")
    @RequiresPermissions("system:domain:remove")
    @Log(title = "组织领域", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(sysOrganizationDomainService.deleteSysOrganizationDomainByIds(ids));
    }


    @ApiOperation("组织领域树形结构")
    @GetMapping("/queryDomainTree")
    public List<SysOrganizationDomain> queryDomainTree(SysOrganizationDomainDto sysOrganizationDomain) {
        List<SysOrganizationDomain> list = sysOrganizationDomainService.selectSysOrganizationDomainList(sysOrganizationDomain);
        return list;
    }


    @ApiOperation("新增子节点")
    @PostMapping("/addChildren")
    public AjaxResult addChildren(@RequestBody SysOrganizationDomainChildren sysOrganizationDomainChildren) {
        if(StringUtils.isEmpty(sysOrganizationDomainChildren.getCode() ) ||  StringUtils.isEmpty( sysOrganizationDomainChildren.getCode())){
            return AjaxResult.error("名称不能为空");
        }
        int result = sysOrganizationDomainService.addChildren(sysOrganizationDomainChildren);
        return result > 0 ? AjaxResult.success("新增成功") : AjaxResult.error("新增失败");
    }


}
